<?php
/**
 * Created by PhpStorm.
 * User: xin6841414
 * Date: 2018/6/6
 * Time: 13:04
 */

namespace App\Modules\Home\Http\Controllers;


use App\Http\Controllers\BasicController;
use App\Modules\Manage\Model\CategoryModel;
use App\Modules\Manage\Model\PigFileUploadModel;
use App\Modules\Manage\Model\PigProductModel;
use App\Modules\Manage\Model\UserModel;
use App\Modules\Manage\Model\PigOrderModel;
//use Illuminate\Support\Facades\DB;
use App\User;
use Illuminate\Http\Request;
use Session;
class IndexController extends BasicController
{

    public function __construct()
    {
        parent::__construct();
        //头部banner显示当前版块效果
        $path = Request()->path();
        $banner = array();
        $banner = [
            ['home/index','网站首页'],
            ['home/classify','供应产品'],
            ['home/news','企业新闻'],
            ['home/people','企业招聘'],
            ['home/contact','联系我们'],
            ['home/order','我的订单'],
        ];
        //判断用户是否登录状态
        if (Request()->session()->has('user')) {
//            echo 1;die;
            $user = json_decode(Request()->session()->get('user'),true);
//            var_export($user);die;
            $this->theme->set('nickname',$user['nickname']);
        }
        $this->theme->set('path',$path);
        $this->theme->set('banner',$banner);
        $this->initTheme('home');
    }

    public function login()
    {
        $param = Request()->except('_token');
        $user = UserModel::login($param['phone']);
        Request()->session()->put('user_id',$user['id']);
        $user = json_encode($user);
        Request()->session()->put('user',$user);
        echo $user;
    }

    public function logout()
    {

    }
    public function index(Request $request)
    {
        //访问量
//        DB::table('pv')->increment('pvNum');
//        $data['newsType'] = NewsController::newsType(3);
//        $data['news'] = NewsController::newsIndex();
//        $data['product_pic'] = ProductController::productIndex();
//        $site_name = ConfigModel::getConfigByAlias('site_name');
//        if($site_name)
//        {
//            $this->theme->setTitle($site_name['rule']);
//        }
        //产品分类导航数据
        $cates = CategoryModel::homeAllCates();
        //产品数据
        $indexProducts = PigProductModel::homeIndexProduct(6);
//        var_export($indexProducts->toArray());
//        die;
        //具体分类产品数据
        $changedProducts = [];
        foreach ($cates as $v) {
            $homeCateProducts = PigProductModel::homeCateProduct($v->id);
            $changedProducts[$v->id] = $homeCateProducts->toArray();
        }
        return $this->theme->scope('home.index',['cates'=>$cates,'indexProducts'=>$indexProducts,'changedProducts'=>$changedProducts])->render();
    }

    public function classify()
    {

        return $this->theme->scope('home.classify')->render();
    }

    public function news()
    {

        return $this->theme->scope('home.news')->render();
    }

    public function people()
    {

        return $this->theme->scope('home.people')->render();
    }

    public function contact() 
    {

        return $this->theme->scope('home.contact')->render();
    }

    public function order()
    {

        return $this->theme->scope('home.order')->render();
    }
    //购物车列表及添加
    public function cart()
    {
        if (Request()->isMethod('get')) {
            $user_id = Request()->session()->get('user_id');
            if (!$user_id) {
                return $this->theme->scope('home/cart-null')->render();
            }
            $carts = PigOrderModel::cartAll($user_id);
            if ($carts) {
//                var_export($carts);die;
                return $this->theme->scope('home.cart',['carts'=>$carts])->render();
            } else {
                return $this->theme->scope('home/cart-null')->render();
            }
        } elseif(Request()->isMethod('post')) {
            $param = Request()->input();
            $product_id = $param['id'];
            $user_id = Request()->session()->get('user_id');
            if (!$user_id) {
                //请先登录
                echo 0;
            }
            $data = [];
            $data['user_id'] = $user_id;
            $data['product_id'] = $product_id;
            $existed = PigOrderModel::existed($data);
            if ($existed) {
            //已加入购物车
                echo 1;
            } else {
            //未加入购物车
                $data['pay_status'] = 0;
                $result = PigOrderModel::cartAdd($data);
                if ($result) {
                    //购物车添加成功；
                    echo 2;
                } else {
                    //购物车添加失败；
                    echo 3;
                }
            }

        }
    }
    //购物车删除
    public function cartDel()
    {
        $param = Request()->except('_token');
        $jsids = json_decode($param['jsids'],true);

        $result = PigOrderModel::cartDel($jsids);
        if ($result) {
            echo 1;
        } else {
            echo 0;
        }
    }
    //空购物车
    public function cartnull()
    {

        return $this->theme->scope('home.cartnull')->render();
    }

    public function detail()
    {
        $id = Request()->id;
        $product = PigProductModel::homeDetailProduct($id);
//    var_export($product->toArray());
//    die;
        return $this->theme->scope('home.detail',['product'=>$product,'id'=>$id])->render();
    }

    public function orderunfinished()
    {
        echo 'unfinished';die;
        return $this->theme->scope('home.orderunfinished')->render();
    }



    public function aboutUs()
    {
        $this->theme->setTitle('关于我们');
        $lists = InformationModel::where('type', 'aboutUs')->orderBy('sort', 'ASC')->get();
        if(!empty($lists))
        {
            $lists = $lists->toArray();
        }
        else
        {
            $lists =[];
        }
        $view = [
          'lists' =>$lists
        ];
       return $this->theme->scope('home.aboutUs', $view)->render();
    }


   public function partner(Request $request)
   {
       $merge = $request->except('_token');
       $this->theme->setTitle('合作伙伴');
       $list = InformationModel::getInfoByType('partner', 6);
       $data =[
           'merge' =>$merge,
           'list'=> $list,
       ];

       return $this->theme->scope('home/partner', $data)->render();
   }
    public function partnerInfo( $id)
    {
        $this->theme->setTitle('合作伙伴');
        if(empty($id))
        {
            return redirect()->back()->with(['error'=>'参数错误！']);
        }
        $info = InformationModel::getInfById($id);
        $data = [
            'info' => $info
        ];
        return $this->theme->scope('home.partnerInfo', $data)->render();
    }


   public function contactUs()
   {
       $this->theme->setTitle('联系我们');
       return $this->theme->scope('home/contactUs')->render();
   }
    public function joinUs()
    {
        $list = InviteManageModel::select('*')->get();
        if(!empty($list))
        {
            $list =  $list->toArray();
            foreach($list as &$v)
            {
                $v['condition'] = explode('&&',$v['condition']);
                $v['position_duty'] = explode('&&',$v['position_duty']);
                $v['position_demand'] = explode('&&',$v['position_demand']);
            }

        }
        else
        {
            $list = [];
        }
        $data = [
            'list' => $list,
        ];
        return $this->theme->scope('home.joinUs', $data)->render();
    }

}